package cn.senjoeson.healthsport.activity;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.senjoeson.healthsport.R;
import cn.senjoeson.healthsport.utils.SPUtils;


public class SetPlanActivity extends AppCompatActivity implements View.OnClickListener {


    public String walk_qty;
    public String achieveTime;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_step_number)
    EditText mTvStepNumber;
    @BindView(R.id.cb_remind)
    CheckBox mCbRemind;
    @BindView(R.id.tv_remind_time)
    TextView mTvRemindTime;
    @BindView(R.id.btn_save)
    Button mBtnSave;
    private String remind;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_exercise_plan);
        ButterKnife.bind(this);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        initData();
        addListener();
    }

    public void initData() {//获取锻炼计划

        String planWalk_QTY = SPUtils.getString("planWalk_QTY", "10000");
        String remind = SPUtils.getString("remind", "1");
        String achieveTime = SPUtils.getString("achieveTime", "20:00");
        if (!planWalk_QTY.isEmpty()) {
            if ("0".equals(planWalk_QTY)) {
                mTvStepNumber.setText("7000");
            } else {
                mTvStepNumber.setText(planWalk_QTY);
            }
        }
        if (TextUtils.isEmpty(remind)) {
            if ("0".equals(remind)) {
                mCbRemind.setChecked(false);
            } else if ("1".equals(remind)) {
                mCbRemind.setChecked(true);
            }
        }

        if (!achieveTime.isEmpty()) {
            mTvRemindTime.setText(achieveTime);
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void addListener() {
        mBtnSave.setOnClickListener(this);
        mTvRemindTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                save();
                break;

            case R.id.tv_remind_time:
                showTimeDialog1();
                break;
        }
    }

    private void save() {
        walk_qty = mTvStepNumber.getText().toString().trim();
//        remind = "";
        if (mCbRemind.isChecked()) {
            remind = "1";
        } else {
            remind = "0";
        }
        achieveTime = mTvRemindTime.getText().toString().trim();
        if (walk_qty.isEmpty() || "0".equals(walk_qty)) {
            SPUtils.saveString("planWalk_QTY", "7000");
        } else {
            SPUtils.saveString("planWalk_QTY", walk_qty);
        }
        SPUtils.saveString("remind", remind);

        if (achieveTime.isEmpty()) {
            SPUtils.saveString("achieveTime", "21:00");
            achieveTime = "21:00";
        } else {
            SPUtils.saveString("achieveTime", achieveTime);
        }
        finish();
    }

    private void showTimeDialog1() {
        final Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
//        String time = tv_remind_time.getText().toString().trim();
        final DateFormat df = new SimpleDateFormat("HH:mm", Locale.CHINA);
//        Date date = null;
//        try {
//            date = df.parse(time);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//
//        if (null != date) {
//            calendar.setTime(date);
//        }
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                String remaintime = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
                Date date = null;
                try {
                    date = df.parse(remaintime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                if (null != date) {
                    calendar.setTime(date);
                }
                mTvRemindTime.setText(df.format(date));
            }
        }, hour, minute, true).show();
    }
}

